﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using MilitaryPluginInterface;

namespace MilitaryPluginInterface
{
    public class ProfessionalAIBrain : AIBrain
    {
        public ProfessionalAIBrain(Military brainOwner)
            : base(brainOwner)
        {
        }

        public override Military  findEnemyForAttack()
        {
            if (_brainOwner.MilitaryForAttack != null)
                if (!_brainOwner.MilitaryForAttack.isDead())
                    return _brainOwner.MilitaryForAttack;
            
            return findStrongestEnemyForAttack();
        }

        public override Military findEnemyForAttack(List<Military> militaries, Military hero)
        {
            if (_brainOwner.MilitaryForAttack != null)
                if (!_brainOwner.MilitaryForAttack.isDead())
                    return _brainOwner.MilitaryForAttack;

            return findStrongestEnemyForAttack(militaries, hero);
        }

        public override Factory findFactoryForAttack()
        {
            if (_brainOwner.MilitaryForAttack != null)
                if (!_brainOwner.MilitaryForAttack.isDead())
                    return _brainOwner.TeamOwner.Factories[0];

            return findRandomFactory();
        }

        private Factory findRandomFactory()
        {
            List<Factory> notDestroyedFactory = new List<Factory>();

            foreach (Team team in _teams)
            {
                if (!team.isLosed())
                {
                    foreach (Factory fac in team.Factories)
                    {
                        if (!fac.isDestroyed())
                            notDestroyedFactory.Add(fac);
                    }
                }
            }

            if (notDestroyedFactory.Count == 0)
                return null;
            else
                return notDestroyedFactory[GameBase.Constant.random.Next() % notDestroyedFactory.Count];
        }

        private Military findStrongestEnemyForAttack(List<Military> militaries, Military hero)
        {
            Military enemy = null;
            float power = -1;

            if (!hero.isDead())
            {
                power = hero.StrengthAttack;
                enemy = hero;
            }

            foreach (Military military in militaries)
            {
                if (power < military.StrengthAttack && !military.isDead())
                {
                    power = military.StrengthAttack;
                    enemy = military;
                }
            }

            return enemy;
        }

        private Military findStrongestEnemyForAttack()
        {
            Military enemy = null;
            float strength = -1;
            foreach (Team team in _teams)
            {
                if (team.TeamName != _brainOwner.TeamOwner.TeamName)
                {
                    foreach (Military military in team.Infantries)
                    {
                        if (military.StrengthAttack > strength)
                        {
                            if (!military.isDead())
                            {
                                strength = military.StrengthAttack;
                                enemy = military;
                            }
                        }
                    }
                }
            }
            return enemy;
        }
    }
}
